<template>
  <el-dialog
    title="修改密码"
    :visible.sync="dialogFormVisible"
    width="40%"
    class="dialog"
  >
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      @submit.native.prevent
    >
      <el-form-item label="密码" prop="pwd">
        <el-input
          type="password"
          v-model="ruleForm.pwd"
          autocomplete="off"
          size="mini"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkpwd">
        <el-input
          type="password"
          v-model="ruleForm.checkpwd"
          autocomplete="off"
          size="mini"
          @keyup.enter.native="submitForm('ruleForm')"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')" size="mini"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  props: {
    id: {
      type: String,
    },
  },
  data() {
    var validatepwd = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkpwd !== "") {
          this.$refs.ruleForm.validateField("checkpwd");
        }
        callback();
      }
    };
    var validatepwd2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      formLabelWidth: "120px",
      dialogFormVisible: false,
      ruleForm: {
        pwd: "",
        checkpwd: "",
      },
      rules: {
        pwd: [{ required: true, validator: validatepwd, trigger: "blur" }],
        checkpwd: [
          { required: true, validator: validatepwd2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            message: "修改成功",
            type: "success",
            duration: 2000,
            showClose: true,
          });
          setTimeout(() => {
            this.submit();
          }, 1000);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    open() {
      this.dialogFormVisible = true;
    },
    submit() {
      this.$axios
        .put("/user/changePsd", {
          password: this.ruleForm.pwd,
          studentNumber: this.id,
        })
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
      this.$emit("close");
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 50%;
}
//让dialog出现在屏幕正中间
.dialog {
  /deep/.el-dialog {
    display: flex;
    flex-direction: column;
    margin: 0 !important;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-height: calc(100% - 10px);
    max-width: calc(100% - 10px);
    height: 50% !important;
    font-size: 0.5rem;
  }
  /deep/.el-dialog .el-dialog__body {
    flex: 1;
    overflow: auto;
  }
}
</style>